package model;

import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.DefaultListModel;

public class LogData {

	/**
	 * INSTANCE 
	 */
	private static LogData logData;
	
	/**
	 * Log Window Width
	 */
	private int logWindowWidth;

	/**
	 * Log Window Height
	 */
	private int logWindowHeight;

	/**
	 * Log Window Name
	 */
	private String logWindowName;

	/**
	 * Log Window Background Color
	 */
	private Color backgroundColor;
	
	private static ArrayList<LogMsg> logs;
	
	private static DefaultListModel logListModel = new DefaultListModel();
	
	private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	
	private boolean isErrorVisible;
	private boolean isInitVisible;
	private boolean isUserActionVisible;
	
	private static ArrayList<Boolean> labelsVisibility;
	
	private LogData() {
		
		logWindowName = "Log";
		backgroundColor = Color.darkGray;
		logs = new ArrayList<LogMsg>();
		logListModel = new DefaultListModel();
		
		isErrorVisible = true;
		isInitVisible = true;
		isUserActionVisible = true;
		
		labelsVisibility = new ArrayList<Boolean>();
		
		labelsVisibility.add(isErrorVisible);
		labelsVisibility.add(isInitVisible);
		labelsVisibility.add(isUserActionVisible);
	}

	public static LogData getInstance() {
		
		if(logData == null) {
			
			logData = new LogData();
		}
		
		return logData;
	}
	
	public static void addLog(LogLabel label, String msg) {
		
		Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);

		LogMsg log = new LogMsg(label, msg, sdf.format(cal.getTime()));
		logs.add(log);
		refreshLogListModel();
	}
	
	private static void refreshLogListModel() {
		
		logListModel.clear();
		
		for(int i = 0; i < logs.size(); i ++) {
			
			boolean tempBoolean = labelsVisibility.get(logs.get(i).getLogLabel()
					.getCode()-1).booleanValue();
			
			if(tempBoolean == true) {
			
				logListModel.add(i, logs.get(i).toString());
			}
		}
	}
	
	public DefaultListModel getLogListModel() {
		return logListModel;
	}

	public int getLogWindowWidth() {
		return logWindowWidth;
	}

	public void setLogWindowWidth(int logWindowWidth) {
		this.logWindowWidth = logWindowWidth;
	}

	public int getLogWindowHeight() {
		return logWindowHeight;
	}

	public void setLogWindowHeight(int logWindowHeight) {
		this.logWindowHeight = logWindowHeight;
	}

	public Color getBackgroundColor() {
		return backgroundColor;
	}

	public void setBackgroundColor(Color backgroundColor) {
		this.backgroundColor = backgroundColor;
	}

	public String getLogWindowName() {
		return logWindowName;
	}

	public void setLogWindowName(String logWindowName) {
		this.logWindowName = logWindowName;
	}
	
	public void setErrorVisible(boolean isErrorVisible) {
		this.isErrorVisible = isErrorVisible;
	}

	public void setInitVisible(boolean isInitVisible) {
		this.isInitVisible = isInitVisible;
	}

	public void setUserActionVisible(boolean isUserActionVisible) {
		this.isUserActionVisible = isUserActionVisible;
	}
	
	
}
